<?php
/**
 *
 * Application for the function: Add Cars
 *
 * This page allows a manager to add information for the new cars.
 *
 *
 * @author Chi Zhang
 * @version 1.0
 * @package dummy-auto
 *
 */

include('../config.inc');
require('dummy-app.inc');

function emp_type($empid) {
        $sql = mysql_query("
                select  Type
                  from  Employee
                 where  EmpID = $empid;
               ");

        if ($sql) {
                $row = mysql_fetch_array($sql);
                return $row[0];
        } else
                exit_404(mysql_error());
}

function get_dealer_num($impid) {
        $sql = mysql_query("
                select  DealerNum
                  from  Employee
                 where  EmpID = $empid;
               ");

        if ($sql) {
                $row = mysql_fetch_array($sql);
                return $row[0];
        } else
                exit_404(mysql_error());
}

session_start();

if ( ! ( session_id() && isset($_SESSION['empid'])
         && emp_type($_SESSION['empid']) == 'M')   )
{
        $tree = make_app("Add Cars :: Invalid User!");
        $app = $tree->app;
        $app->addChild('p', "Only Authorized Manager (Employee) Can Use This Function.");

        if (! ( session_id() && isset($_SESSION['empid'])) )
                session_end();

} elseif ($_GET['update']) {

        if (! ( $_GET['mod'] && $_GET['vin'] && $_GET['plan'] &&
                $_GET['make'] && $_GET['model'] && $_GET['style'] &&
                $_GET['year'] && isset($_GET['mile']) && $_GET['price'] &&
                $_GET['door'] && isset($_GET['ac']) &&
                isset($_GET['pwing']) && isset($_GET['plock']) &&
                isset($_GET['fourwd']) && $_GET['color']
                    ) )
                exit_404('All field need to be entered');

        $get_mod = "
                select  CarModelNum
                  from  Car_Model
                 where  CarModelNum = '$_GET[mod]';
        ";
        $get_vin = "
                select  VIN
                  from  Car
                 where  VIN = '$_GET[vin]';
        ";
        $get_plan = "
                select  Plan
                  from  Feature_Plan
                 where  Plan = $_GET[plan];
        ";
        $get_match = "
                select  VIN, Plan
                  from  Car_Feature
                 where  VIN = '$_GET[vin]'
                   and  Plan = $_GET[plan];
        ";

        if (! ($sql_list_1 = mysql_query($get_mod)) ) exit_404(mysql_error());
        if (! ($sql_list_2 = mysql_query($get_vin)) ) exit_404(mysql_error());
        if (! ($sql_list_3 = mysql_query($get_plan)) ) exit_404(mysql_error());
        if (! ($sql_list_4 = mysql_query($get_match)) ) exit_404(mysql_error());

        $changed = false;

        if (! mysql_num_rows($sql_list_1)) {

                if (! mysql_query("
                        insert into Car_Model value (
                        '$_GET[mod]', '$_GET[make]',
                        '$_GET[model]', '$_GET[style]');
                            ") ) exit_404(mysql_error());
                $changed = true;
        }
        if (! mysql_num_rows($sql_list_2)) {

                $tmp = get_dealer_num($_SESSION['empid']);
                if (! mysql_query("
                        insert into Car value (
                        '$_GET[vin]', '$_GET[mod]', '$tmp', 
                        '$_GET[year]', $_GET[mile], $_GET[price]);
                            ") ) exit_404(mysql_error());
                $changed = true;
        }
        if (! mysql_num_rows($sql_list_3)) {

                if (! mysql_query("
                        insert into Feature_Plan value (
                        $_GET[plan], $_GET[door], $_GET[ac],
                        $_GET[pwing], $_GET[plock],
                        $_GET[fourwd], '$_GET[color]');
                            ") ) exit_404(mysql_error());               
                $changed = true;
        }
        if ((!mysql_num_rows($sql_list_2)) || (!mysql_num_rows($sql_list_3)) ||
            (!mysql_num_rows($sql_list_4))
                ) {
                if (! mysql_query("
                        insert into Car_Feature
                        value ('$_GET[vin]', $_GET[plan]);
                            ") ) exit_404(mysql_error());               
                $changed = true;
        }
        if ( $changed ) {
                if (! mysql_query("commit;")) exit_404(mysql_error());

                $tree = make_app("Add Cars :: Update Successful");
                $app = $tree->app;
                $app->addChild('h2', "New car information have been updated successfully!");
        } else {
                $tree = make_app("Add Cars :: Update Failed");
                $app = $tree->app;
                $app->addChild('h2', "Some error occurs in updating the data!");
        }

        $app->addChild('input');
        $app->input['type'] = 'button';
        $app->input['value'] = 'Finish';
        $app->input['onclick'] = "load_app('manager')";

} else {

        $tree = make_app("Add Cars");
        $app = $tree->app;
        $app->h2 = "Add a new car:";
        $app->p = "Please enter the following information:";

        $app->form['action'] = 'app/manager.php';
        $form = $app->form;

        $form->div[0]->label[] = 'Model Number: ';
        add_child($form->div[0], 'input',
                  array('name' => 'mod', 'type' => 'text', 'maxlength' => '5')
                );
        $form->div[0]['id'] = 'manager_left';
        $form->div[0]['class'] = 'layout_left';

        $form->div[1]->label[] = 'Car VIN: ';
        add_child($form->div[1], 'input',
                  array('name' => 'vin', 'type' => 'text', 'maxlength' => '8')
                );
        $form->div[1]['id'] = 'manager_right';
        $form->div[1]['class'] = 'layout_right';

        $form->div[0]->label[] = 'Make: ';
        add_child($form->div[0], 'input',
                  array('name' => 'make', 'type' => 'text')
                );

        $form->div[0]->label[] = 'Model: ';
        add_child($form->div[0], 'input',
                  array('name' => 'model', 'type' => 'text')
                );

        $form->div[0]->label[] = 'Style: ';
        add_child($form->div[0], 'input',
                  array('name' => 'style', 'type' => 'text')
                );

        $form->div[1]->label[] = 'Year: (yyyy-mm-dd)';
        add_child($form->div[1], 'input',
                  array('name' => 'year', 'type' => 'text', 'maxlength' => '10')
                );

        $form->div[1]->label[] = 'Mileage: ';
        add_child($form->div[1], 'input',
                  array('name' => 'mile', 'type' => 'text')
                );

        $form->div[1]->label[] = 'Sticker Price: ';
        add_child($form->div[1], 'input',
                  array('name' => 'price', 'type' => 'text')
                );

        $form->label[] = 'Feature Plan: ';
        add_child($form, 'input',
                  array('name' => 'plan', 'type' => 'text', 'maxlength' => '4')
                );

        $form->label[] = 'Number of Doors: ';
        add_child($form, 'input',
                  array('name' => 'door', 'type' => 'text', 'maxlength' => '1')
                );

        $form->label[] = 'AC: (0 or 1)';
        add_child($form, 'input',
                  array('name' => 'ac', 'type' => 'text', 'maxlength' => '1')
                );

        $form->label[] = 'Power Wing: (0 or 1)';
        add_child($form, 'input',
                  array('name' => 'pwing', 'type' => 'text', 'maxlength' => '1')
                );

        $form->label[] = 'Power Lock: (0 or 1)';
        add_child($form, 'input',
                  array('name' => 'plock', 'type' => 'text', 'maxlength' => '1')
                );

        $form->label[] = '4 WD: (0 or 1)';
        add_child($form, 'input',
                  array('name' => 'fourwd', 'type' => 'text', 'maxlength' => '1')
                );

        $form->label[] = 'Color: ';
        add_child($form, 'input',
                  array('name' => 'color', 'type' => 'text')
                );


        $form->addChild('p')->addChild('input');
        $form->p->input['type'] = "submit";
        $form->p->input['name'] = "update";
        $form->p->input['value'] = "submit";

        $form->p->addChild('input');
        $form->p->input[1]['type'] = "reset";
        $form->p->input[1]['value'] = "reset";
}

echo $tree->asXML();
?>
